﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.ContentManagement;
using Ruitu.Edu.TrainingDemo.Models;
using OrchardCore.Modules;
using OrchardCore.ContentFields.Fields;

namespace Ruitu.Edu.TrainingDemo.Controllers
{
    public class PersonListController : Controller
    {
        private readonly IContentManager _contentManager;
        private readonly IClock _clock;

        public PersonListController(
            IContentManager contentManager,
            IClock clock)
        {
            _contentManager = contentManager;
            _clock = clock;
        }

        [HttpGet, Route("api/persons")]
        public async Task<string> CreateAnAndroid()
        {
            var personPage = await _contentManager.NewAsync("PersonPage");
            var name = "tangxiaoyu";

            personPage.Alter<PersonPart>(part =>
            {
                part.Name = name;
                part.Age = 18;
                part.Sex = "男";
                part.BirthDateUtc = _clock.UtcNow;
                part.Handedness = Handedness.Right;
            });

            var personPart = personPage.As<PersonPart>();
            personPart.Alter<TextField>(nameof(PersonPart.Biography),field =>
            {
                field.Text = "我现在有感知能力了！";
            });

            await _contentManager.CreateAsync(personPage, VersionOptions.Draft);
            await _contentManager.UpdateAsync(personPage);
            await _contentManager.PublishAsync(personPage);

            return $"创建了一个名为{name}的Android人。";

        }
    }
}
